{#<div class="comments" id="comments">#}
{#    <h3>{{ pagination.total }} 条评论#}
{# 展示最近的评论 #}
{#        <small>#}
{# 传递当前页数 #}
{#            <a href="{{ url_for('main.show_post', post_id=post.id, page=pagination.pages or 1) }}#comments">#}
{#                最近</a>#}
{#        </small>#}
{# 验证用户登录#}
{#        {% if current_user.is_authenticated %}#}
{#  只有文章的作者才能设置评论的功能  #}
{#            {% if current_user == post.author %}#}
{#                <form class="float-right" method="post"#}
{#                      action="{{ url_for('main.set_comment',post_id=post.id, next=request.full_path) }}">#}
{# 用户是否关闭评论的功能 #}
{#                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>#}
{#                    <button type="submit" class="btn btn-warning btn-sm">#}
{#                        {% if post.can_comment %}关闭{% else %}启动{% endif %} 评论#}
{#                    </button>#}
{#                </form>#}
{#            {% endif %}#}
{#        {% endif %}#}
{#    </h3>#}
{#    {% if comments %}#}
{#        <ul class="list-group">#}
{# 遍历所有评论 #}
{#            {% for comment in comments %}#}
{#                <li class="list-group-item list-group-item-action flex-column">#}
{#                    <div class="d-flex w-100 justify-content-between">#}
{#                        <h5 class="mb-1">#}
{#                            <a href="{% if comment.site %}{{ comment.site }}{% else %}#{% endif %}"#}
{#                               target="_blank">#}
{#                                            判断评论是否来自管理员#}
{#                                {% if comment.from_admin %}#}
{#                                    {{ admin.name }}#}
{#                                {% else %}#}
{#                                    {{ comment.author }}#}
{#                                {% endif %}#}
{#                            </a>#}
{#                            {% if comment.from_admin %}#}
{#                                <span class="badge badge-primary">作者</span>{% endif %}#}
{#                            {% if comment.replied %}<span class="badge badge-light">回复</span>{% endif %}#}
{#                        </h5>#}
{#                        <small data-toggle="tooltip" data-placement="top" data-delay="500"#}
{#                               data-timestamp="{{ comment.timestamp.strftime('%Y-%m-%dT%H:%M:%SZ') }}">#}
{#                            {{ moment(comment.timestamp).fromNow() }}#}
{#                        </small>#}
{#                    </div>#}
{#    判断是否回复了别的评论#}
{#                    {% if comment.replied %}#}
{#                        <p class="alert alert-dark reply-body">{{ comment.replied.author }}:#}
{#                            <br>{{ comment.replied.body }}#}
{#                        </p>#}
{#                    {%- endif -%}#}
{#                    <p class="mb-1">{{ comment.body }}</p>#}
{#                    <div class="float-right">#}
{#                        <a class="btn btn-light btn-sm"#}
{#                           href="{{ url_for('.reply_comment', comment_id=comment.id) }}">回复</a>#}
{#                        {% if current_user.is_authenticated %}#}
{#                            <a class="btn btn-light btn-sm" href="mailto:{{ comment.email }}">电子邮件</a>#}
{#                                        删除评论#}
{#                            <form class="inline" method="post"#}
{#                                  action="#">#}
{#                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>#}
{#                                <button type="submit" class="btn btn-danger btn-sm"#}
{#                                        onclick="return confirm('你确定删除这个评论吗?');">删除#}
{#                                </button>#}
{#                            </form>#}
{#                        {% endif %}#}
{#                    </div>#}
{#                </li>#}
{#            {% endfor %}#}
{#        </ul>#}
{#    {% else %}#}
{#        <div class="tip"><h5>暂无评论!</h5></div>#}
{#    {% endif %}#}
{#</div>#}
{#{% if comments %}#}
{# 评论分页 #}
{#    {{ render_pagination(pagination, fragment='#comments') }}#}
{#{% endif %}#}
{#{% if request.args.get('reply') %}#}
{#    <div class="alert alert-dark">#}
{#        正在回复<strong>{{ request.args.get('author') }}</strong>:#}
{#        <a class="float-right" href="{{ url_for('main.show_post', post_id=post.id) }}">取消回复</a>#}
{#    </div>#}
{#{% endif %}#}
{# 判断是否能发评论 #}
{#{% if post.can_comment %}#}
{#    <div id="comment-form">#}
{#        {{ render_form(form, action=request.full_path) }}#}
{#    </div>#}
{#{% else %}#}
{#    <div class="tip"><h5>评论已被禁止!</h5></div>#}
{#{% endif %}#}

<div class="comments" id="comments">
    <h3>{{ post.comments|length }} 条评论
        {# 展示最近的评论 #}
        <small>
            <a href="{{ url_for('main.show_post', post_id=post.id, page=pagination.pages or 1) }}#comment-form">最近</a>
        </small>
        {#  只有文章的主人才有设置评论的功能 #}
        {% if current_user == post.author %}
            {# 设置评论的功能 #}
            <form class="inline" method="post" action="{{ url_for('main.set_comment', post_id=post.id) }}">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="btn btn-xs btn-link float-right">
                    {% if post.can_comment %}关闭{% else %}开启{% endif %}评论
                </button>
            </form>
        {% endif %}
    </h3>
    <hr>
    {% if comments %}
        {# 展示评论页面 #}
        {% for comment in comments %}
            <div class="comment">
                {# 使用ajax悬浮到用户图片显示用户信息 #}
                <div class="comment-thumbnail">
                    <a href="{{ url_for('user.index', username=comment.author.username) }}">
                        <img class="rounded img-fluid avatar-s profile-popover"
                             data-href="{{ url_for('ajax.get_profile', user_id=comment.author.id) }}"
                             src="{{ url_for('main.get_avatar', filename=comment.author.avatar_m) }}">
                    </a>
                </div>
                <div class="comment-body">
                    <h6>
                        {# 使用ajax悬浮用户名称显示用户信息 #}
                        <a class="profile-popover"
                           data-href="{{ url_for('ajax.get_profile', user_id=comment.author.id) }}"
                           href="{{ url_for('user.index', username=comment.author.username) }}">
                            {{ comment.author.name }}
                        </a>
                        {% if comment.author == post.author %}
                            <span class="badge badge-light">作者</span>
                        {% endif %}
                        <small data-toggle="tooltip" data-placement="top" data-timestamp="{{ comment.timestamp }}"
                               data-delay="500">
                            {{ moment(comment.timestamp).fromNow(refresh=True) }}
                        </small>
                        {# 展示评论 #}
                        {% if current_user.is_authenticated %}
                            <span class="float-right">
                            <span class="dropdown">
                                <button class="btn btn-sm btn-light" type="button" id="dropdownMenuButton"
                                        data-toggle="dropdown"
                                        aria-haspopup="true" aria-expanded="false">
                                <span class="oi oi-ellipses"></span>
                                </button>
                                <span class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                    {# 只有非这条评论的人才能回复 #}
                                    {% if current_user != comment.author %}
                                        <a class="dropdown-item btn"
                                           href="{{ url_for('main.reply_comment', comment_id=comment.id) }}">
                                        <span class="oi oi-comment-square"></span> 回复
                                        </a>
                                    {% endif %}
                                    {# 确定删除评论的人 #}
                                    {% if current_user == comment.author or current_user == post.author or current_user.can('MODERATE') %}
                                        {#                                        <a class="dropdown-item" data-toggle="modal"#}
                                        {#                                           href="#"#}
                                        {#                                           data-href="{{ url_for('main.delete_comment', comment_id=comment.id) }}"#}
                                        {#                                           data-target="#confirm-delete">#}
                                        {#                                            <span class="oi oi-trash" aria-hidden="true"></span> 删除#}
                                        {#                                        </a>#}
                                        <form class="inline" method="post"
                                              action="{{ url_for('main.delete_comment', comment_id=comment.id) }}">
                                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                                            <button type="submit" class="dropdown-item"
                                                    onclick="return confirm('你确定删除评论吗?');"><span class="oi oi-trash" aria-hidden="true"></span>删除</button>
                                        </form>
                                    {% endif %}
                                    {# 确定能举报评论的人 #}
                                    {% if current_user != comment.author %}
                                        <form class="inline" method="post"
                                              action="{{ url_for('main.report_comment', comment_id=comment.id) }}">
                                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                            <button type="submit" class="dropdown-item">
                                                <span class="oi oi-warning" aria-hidden="true"></span> 举报
                                            </button>
                                        </form>
                                    {% endif %}
                                </span>
                                </span>
                            </span>
                        {% endif %}
                    </h6>
                    <p>
                        {# 展示被评论的评论 #}
                        {% if comment.replied %}
                            <p class="alert alert-dark reply-body">{{ comment.replied.author.name }}:
                                <br>{{ comment.replied.body }}
                            </p>
                        {% endif %}
                    <p class="mb-1">
                        {{ comment.body }}
                    </p>
                    </p>
                </div>
            </div>
            <hr>
        {% endfor %}
        <div class="page-footer">
            {#  为评论进行分页设置 #}
            {{ render_pagination(pagination) }}
        </div>
    {% else %}
        <p class="tip">暂无评论!</p>
    {% endif %}

    {% if post.can_comment %}
        {% if current_user.is_authenticated %}
            {% if current_user.can('COMMENT') %}
                {% if request.args.get('reply') %}
                    <div class="alert alert-dark">
                        向 {{ request.args.get('author') }} 回复:
                        <a class="float-right" href="{{ url_for('main.show_post', post_id=post.id) }}">取消</a>
                    </div>
                {% endif %}
                <div class="comment-form-area">
                    <div class="comment-form-thumbnail">
                        <img class="rounded img-fluid avatar-s"
                             src="{{ url_for('main.get_avatar', filename=current_user.avatar_m) }}">
                    </div>
                    <div class="comment-form" id="comment-form">
                        {{ render_form(form, action=url_for('main.new_comment', post_id=post.id, page=pagination.pages or 1,
                        reply=request.args.get('reply')),
                        extra_classes="text-right") }}
                    </div>
                </div>
            {% else %}
                <p class="tip">
                    权限不足!
                </p>
            {% endif %}
        {% else %}
            <p class="tip">
                <a href="{{ url_for('auth.login') }}">登录</a>
                或
                <a href="{{ url_for('auth.register') }}">注册</a> 留下评论!
            </p>
        {% endif %}
    {% else %}
        <p class="tip">
            评论功能已禁止!
        </p>
    {% endif %}
</div>
